package me.zhengjie.controller;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.domain.ExceptionalService;
import me.zhengjie.domain.TaskResultService;
import me.zhengjie.dto.MesMessage;
import me.zhengjie.dto.Resp;
import me.zhengjie.dto.sqlServer.InBoundMission;
import me.zhengjie.repository.StorageRepository;
import me.zhengjie.repository.TaskResultRepository;
import me.zhengjie.service.StorageService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "错误恢复")
@RequestMapping("/api/errorRecovery")
public class ErrorRecoveryController {

    private final TaskResultRepository taskResultRepository;
    private final ExceptionalService exceptionalService;
    private final StorageService storageService;
    private final TaskResultService taskResultService;



    /*@Log("接收入库任务反馈失败恢复")
    @ApiOperation("接收入库任务反馈失败恢复")
    @GetMapping(value = "/inBoundTask")
    @Transactional(rollbackFor = Exception.class)
    public Resp errorReceiveInBoundTaskFeedback(){

        return null;
    }*/

    @Log("查询已经创建等待执行的任务")
    @ApiOperation("查询已经创建等待执行的任务")
    @GetMapping(value = "/inBoundTask")
    public Resp findNowInBoundMission(){
        List<InBoundMission> nowInBoundMission = exceptionalService.findNowInBoundMission();
        if(!nowInBoundMission.isEmpty()){
            return Resp
                    .builder()
                    .msg("查询成功")
                    .data("inboundMission",exceptionalService.findNowInBoundMission().get(0))
                    .status(200)
                    .build();
        }
        return Resp
                .builder()
                .msg("当前没有入库任务产生")
                .status(400)
                .build();
    }

    @Log("恢复入库扫码口错误")
    @ApiOperation("恢复入库扫码口错误")
    @GetMapping(value = "/recoveryInboundError/{missionCode}")
    public Resp RecoveryInboundError(@PathVariable("missionCode") String missionCode){
        List<InBoundMission> nowInBoundMission = exceptionalService.findNowInBoundMission();
        if(!nowInBoundMission.isEmpty()){
            String storageCode = nowInBoundMission.get(0).getDestination();
            storageService.cleanStorage(storageCode);
            Integer i = exceptionalService.deleteInBoundMission(missionCode);
            if (i==1){
                return Resp
                        .builder()
                        .msg("恢复成功")
                        .status(200)
                        .build();
            }else {
                return Resp
                        .builder()
                        .msg("操作失败")
                        .status(200)
                        .build();
            }
        }
        return Resp
                .builder()
                .msg("当前没有入库任务产生")
                .status(400)
                .build();
    }

    @Log("恢复出入库卡任务")
    @ApiOperation("恢复出入库卡任务")
    @AnonymousAccess
    @GetMapping(value = "/recoveryPauseRunning/{missionCode}")
    public Resp recoveryPauseRunning(@PathVariable("missionCode") String missionCode){
        MesMessage mesMessage = taskResultService.commitFinish(missionCode);
        if(!ObjectUtil.isEmpty(mesMessage)){
            return Resp
                    .builder()
                    .data("mesMessage",mesMessage)
                    .status(400)
                    .build();
        }
        return Resp
                .builder()
                .msg("操作成功")
                .status(200)
                .build();
    }
}
